package cn.gz.ibix.load;

import android.os.Handler;
import android.os.Looper;

import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.JsonElement;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.gz.ibix.util.CallBack;
import cn.gz.ibix.util.HttpUtil;
import cn.gz.ibix.util.RequestUtil;

/**
 * 项目名称：QiYiChuAPP
 * <br>类描述：
 * <br>创建人：htliu
 * <br>创建时间：2018/3/24 18:19
 * <br>修改人:
 * <br>修改时间：2018/3/24 18:19
 * <br>修改备注：
 * <br>@version
 */
public abstract class BaseDataLoader<T> {
    private static final Handler uiHandler = new Handler(Looper.getMainLooper());
    protected final String TAG = getClass().getSimpleName();
    private final Object LOCK = new Object();
    protected int pageSize = 10;
    protected Gson gson = new Gson();
    protected int page = 0;
    private List<T> list = Lists.newArrayList();
    private Listener<T> listener;
    private boolean loading = false;
    private boolean end = false;


    public void setListener(Listener<T> listener) {
        this.listener = listener;
    }

    public boolean isEnd() {
        return end;
    }

    public boolean isLoading() {
        return loading;
    }

    public void load() {
        load(new HashMap<String, String>());
    }

    public void load(Map<String, String> params) {
        if (loading || end) {
            return;
        }
        loading = true;
        editParam(params);
        if (usePage()) {
            params.put("page", String.valueOf(page + 1));
            params.put("pageSize", String.valueOf(pageSize));
        }
        RequestUtil requestUtil = new RequestUtil();
        requestUtil.api(api()).params(params)
                .callBack(new CallBack() {
                    @Override
                    public void onSuccess(JsonElement data) {
                        List<T> dataList = processData(data);
                        synchronized (LOCK) {
                            end = dataList.isEmpty();
                            if (usePage()) {
                                int currentPage = data.getAsJsonObject().get("currentPage").getAsInt();
                                page = currentPage;
                                if (currentPage <= 1) {
                                    list.clear();
                                }
                            } else {
                                end = true;
                            }
                            list.addAll(dataList);
                        }
                        uiHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                loading = false;
                                listener.success(list);
                            }
                        });
                    }

                    @Override
                    public void onFail(Exception e) {
                        loading = false;
                        listener.fail(e);
                    }
                });
        List<T> dataList = processData(null);
        synchronized (LOCK) {
            end = dataList.isEmpty();
            list.addAll(dataList);
        }
        uiHandler.post(new Runnable() {
            @Override
            public void run() {
                loading = false;
                listener.success(list);
            }
        });
        HttpUtil.getInstance().request(requestUtil);

    }


    protected boolean usePage() {
        return true;
    }

    protected abstract String api();

    protected abstract List<T> processData(JsonElement data);

    protected abstract void editParam(Map<String, String> params);

    public interface Listener<T> {
        void success(List<T> list);

        void fail(Exception e);
    }

}
